/**
 * 
 */
package org.jenkon.factory;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import org.jenkon.utils.ClassUtils;

/**
 * @author Jens Konopka
 * 
 */
public class MapFactory {

	/**
	 * @return a new created instance of {@link HashMap}.
	 */
	public static <K, V> Map<K, V> create() {
		return create(HashMap.class);
	}

	/**
	 * <p>
	 * Create a new instance of a specific {@link Class} or interface of {@link Map}.
	 * </p>
	 * <p>
	 * If the parameter is a interface, then check is it a {@link Map}, {@link SortedMap} or other.<br>
	 * If it is a {@link Map} then create a new instance of {@link LinkedHashMap}.<br>
	 * If it is a {@link SortedMap} then create a new instance of {@link TreeMap}.<br>
	 * Else it does create a new instance of {@link HashMap}.<br>
	 * It is not a interface then create a new instance of parameter type.<br>
	 * </p>
	 * 
	 * @param type the {@link Class} to create a new instance.
	 * @return a new created instance of {@link Map} with a specific class of interface type
	 * @throws NullPointerException if the parameter is <code>null</code>.
	 * @throws IllegalArgumentException if the parameter is not valid.
	 */
	public static <K, V> Map<K, V> create(final Class<?> type) {
		if (type.isInterface()) {
			if (Map.class.equals(type)) {
				return new LinkedHashMap<K, V>();
			} else if (SortedMap.class.equals(type)) {
				return new TreeMap<K, V>();
			} else {
				return new HashMap<K, V>();
			}
		} else {
			return ClassUtils.newInstance(type);
		}
	}

}
